package com.team.bus.module.file.service;

import com.team.bus.module.file.dao.IFileDao;
import com.team.bus.module.file.model.File;
import com.team.bus.module.file.model.FileRecord;
import com.team.bus.module.file.pagemodel.PFiler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zjframework.traceframe.common.dao.util.StringUtil;
import org.zjframework.traceframe.common.enums.DateStyle;
import org.zjframework.traceframe.common.model.DataGrid;
import org.zjframework.traceframe.common.service.impl.CommonServiceImpl;
import org.zjframework.traceframe.common.util.DateUtil;
import org.zjframework.traceframe.common.util.JSONUtil;

import java.util.*;

/**
 * Created by John on 2018/3/28.
 */
@Service("fileService")
public class FileServiceImpl extends CommonServiceImpl implements IFileService {
    @Autowired
    private IFileDao fileDao;

    @Override
    public JSONUtil add(File file)
    {
        JSONUtil json = new JSONUtil();
        try{
            if(StringUtil.isNotEmpty(file))
            {
                file.setIsindefile(file.getCopies());
                file.setNumber(file.getNumberpart_1()+"〔"+file.getNumberpart_2()+"〕"+file.getNumberpart_3()+"号");
                file = fileDao.saveInsertRow(file);
                json.setjson(true,"添加成功",file);
            }
        }catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
        }


        return json;
    }
    @Override
    public JSONUtil edit(File file)
    {
        JSONUtil json = new JSONUtil();
        try{
            if(StringUtil.isNotEmpty(file))
            {
                file.setNumber(file.getNumberpart_1()+"〔"+file.getNumberpart_2()+"〕"+file.getNumberpart_3()+"号");
                file = fileDao.updateRow(file);
                json.setjson(true,"修改成功",file);
            }
        }catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
        }
        return json;
    }
    @Override
    public DataGrid<PFiler> tree(String name,String number,String reader,String isindefile)
    {
        String hql = "from File where 1=1";
        Map<String,Object> param = new HashMap<>();
        if(StringUtil.isNotEmpty(name)){
            hql += " and name like:name ";
            param.put("name", "%%"+ name + "%%");
        }
        if(StringUtil.isNotEmpty(number)){
            hql += " and number like:number ";
            param.put("number", "%%"+ number + "%%");
        }
        if(StringUtil.isNotEmpty(reader)){
            hql += " and reader like:reader ";
            param.put("reader", "%%"+ reader + "%%");
        }
        if(StringUtil.isNotEmpty(isindefile)){
            if(Integer.parseUnsignedInt(isindefile) == 1)
            {hql += " and isindefile > 0 ";}
            else
            {
                hql += " and isindefile < 1 ";
            }
        }
        hql += " order by number";
        List<File> files = fileDao.findHql(hql,param);
        List<PFiler> pFilers = new ArrayList<>();
        for(File f:files)
        {
            PFiler pFiler = new PFiler();
            BeanUtils.copyProperties(f,pFiler);
            if(f.getIsindefile()>0)
            {
                pFiler.setIsindefile_str("在档"+f.getIsindefile()+"份");
            }else
            {
                pFiler.setIsindefile_str("未在档");
            }
            pFilers.add(pFiler);
        }
        DataGrid<PFiler> dataGrid = new DataGrid<>();
        dataGrid.setRows(pFilers);
        dataGrid.setTotal(pFilers.size());
        return dataGrid;
    }
    @Override
    public DataGrid<FileRecord> recordtree(String filename,String start,String end)
    {
        String hql = "from FileRecord where 1=1";
        Map<String,Object> param = new HashMap<>();
        if(StringUtil.isNotEmpty(filename)){
            hql += " and filename like:filename ";
            param.put("filename", "%%"+ filename + "%%");
        }
        if(StringUtil.isNotEmpty(start))
        {
            hql += " and date>=:start";
            param.put("start",start);
        }
        if(StringUtil.isNotEmpty(end))
        {
            hql += " and date<=:end";
            param.put("end",end);
        }
        hql+= " order by date desc";
        List<FileRecord> fileRecords = fileDao.findHql(hql,param);
        DataGrid<FileRecord> dataGrid = new DataGrid<>();
        dataGrid.setRows(fileRecords);
        dataGrid.setTotal(fileRecords.size());
        return dataGrid;
    }
    @Override
    public JSONUtil del(String ids)
    {
        JSONUtil json = new JSONUtil();
        StringBuffer hql = new StringBuffer();
        hql.append("delete File where id=");
        hql.append("'"+ids+"'");
        try{
            fileDao.executeHql(hql.toString());
            json.setjson(true,"删除成功",0);
        }catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
        }
        return json;
    }
    @Override
    public JSONUtil jieyue(String id,String borrower,String number,int copies)
    {
        JSONUtil json = new JSONUtil();
        try {
            File file = fileDao.get(File.class, id);
            if(file.getIsindefile() - copies >= 0){
                FileRecord fileRecord = new FileRecord();
                fileRecord.setFid(id);
                fileRecord.setCopies(copies);
                fileRecord.setNumber(number);
                fileRecord.setFilename(file.getName());
                fileRecord.setBorrower(borrower);
                fileRecord.setDate(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
                fileRecord.setType("借出");
                fileRecord = fileDao.saveInsertRow(fileRecord);
                file.setIsindefile(file.getIsindefile()-copies);
                file = fileDao.updateRow(file);
                json.setjson(true,"操作成功",0);
            }else{
                json.setjson(false,"该档案借阅份数不足，操作失败",0);
            }
        }catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
        }
        return json;
    }
    @Override
    public JSONUtil guihuan(String id,String borrower,String number,int copies)
    {
        JSONUtil json = new JSONUtil();
        try {
            File file = fileDao.get(File.class, id);
            if(file.getIsindefile() + copies <= file.getCopies()){
                FileRecord fileRecord = new FileRecord();
                fileRecord.setFilename(file.getName());
                fileRecord.setNumber(number);
                fileRecord.setBorrower(borrower);
                fileRecord.setFid(id);
                fileRecord.setCopies(copies);
                fileRecord.setDate(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
                fileRecord.setType("归档");
                fileRecord = fileDao.saveInsertRow(fileRecord);
                file.setIsindefile(file.getIsindefile()+copies);
                file = fileDao.updateRow(file);
                json.setjson(true,"操作成功",0);
            }else{
                json.setjson(false,"超出总份数，操作失败",0);
            }
        }catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
        }
        return json;
    }

}
